<?php
class Model_Service
{
	protected  $db;

	public function __construct()
	{
		$this->db = Zend_Registry::get("db");
	}
	
	public function getAllService()
	{		
		$ip=Model_ActionLog::getIp();
		
		$select  = $this->db->select()->from('acc_service');
		$select->where('status = ?', 'Active');
		if (($ip=='70.50.227.211')or($ip=='199.21.151.11')or($ip=='127.0.0.1')){ // in office
			//$select->orWhere('office_only = ?', 'Yes');
		}else{
			$select->orWhere('office_only = ?', 'No');			
		}
		$select->order('group_id asc');
		//$select->order('service_name asc');
		$select->order('group_order asc');
		
		$result = $this->db->fetchAll($select);
		//App_Log::query($this->db);

		return $result;
	}
	
	public function getServiceInfo($dpt_id)
	{		
		$select  = $this->db->select()->from('acc_service');
		$select->where('status = ?', 'Active');
		$select->where('dpt_id = ?', (int)$dpt_id);
		
		$row = $this->db->fetchRow($select);
		//App_Log::query($this->db);

		return $row;
	}

	public function getAllGroup()
	{		
		$select  = $this->db->select()->from('acc_group');
		$select->where('status = ?', 'Active');
		$select->order('group_id asc');
		
		$result = $this->db->fetchAll($select);
		//App_Log::query($this->db);

		return $result;
	}	
	
	public function getLoginArea($dpt_id, $dpt_account_id, $client_id)
	{		
		$select  = $this->db->select()->from('acc_client_service', 'login_area');	
		$select->where('dpt_id = ?', (int)$dpt_id);
		$select->where('dpt_account_id = ?', $dpt_account_id);
		$select->where('client_id = ?', $client_id);
		
		$login_area = $this->db->fetchOne($select);
		//App_Log::query($this->db);

		return $login_area;
	}

	
	public function addService($dpt_id,$dpt_name,$description,$service_url,$service_name, $service_area,$icon,$transfer_fund_allowed,$network_id,$payment_center_id)
	{
		$data = array(
            'dpt_id' => $dpt_id,
            'dpt_name' => $dpt_name,
            'description' => $description,
			      'service_url' => $service_url,
            'service_name' => $service_name,
            'service_area' => $service_area,
            'icon' => $icon,
            'transfer_fund_allowed' => $transfer_fund_allowed,
            'network_id' => $network_id,
            'payment_center_id' => $payment_center_id
		);
		
		$result = $this->db->insert('acc_service',$data);
		//App_Log::query($this->db);
		
		return $result->rowCount();		
	}

	function updateService($dpt_id,$dpt_name,$description,$service_url,$service_name, $service_area,$icon,$transfer_fund_allowed,$network_id,$payment_center_id)
	{
		$where = array('dpt_id = ?' => (int)$dpt_id);

		$data['dpt_name']=$dpt_name;
		$data['description']=$description;
		$data['service_url']=$service_url;
		$data['service_name']=$service_name;
		$data['service_area']=$service_area;
		$data['icon']=$icon;
		$data['transfer_fund_allowed']=$transfer_fund_allowed;
		$data['network_id']=$network_id;
		$data['payment_center_id']=$payment_center_id;
		
		$updated = $this->db->update('acc_service', $data, $where);
		//App_Log::query($this->db);

		if ($updated) return true;
		else return false;
	}	
	
	public function getClientServiceInfo($dpt_id, $dpt_account_login)
	{
	   $select  = $this->db->select()->from('acc_client_service');
	   $select->where('dpt_id = ?', (int)$dpt_id);
	   $select->where('dpt_account_login = ?', $dpt_account_login);
	   $select->order('link_id ASC');
     
	   $rows = $this->db->fetchAll($select);
	   $num = count($rows);
	   if ($num == 1) $client_id = $rows[0]['client_id'];
	   elseif ($num == 0) $client_id = -1;  // not subscribed
	   else $client_id = -2;  // subscribed by more than one account.
	   
	   //App_Log::query($this->db);
	   return $client_id;
	}
	
	public function getClientServiceById($dpt_id, $dpt_account_id)
	{
	   $select  = $this->db->select()->from('acc_client_service');
	   $select->where('dpt_id = ?', (int)$dpt_id);
	   $select->where('dpt_account_id = ?', $dpt_account_id);
	   $select->order('link_id ASC');
     
	   $rows = $this->db->fetchAll($select);

	   return $rows;
	}
	

	public function getClientDptService($dpt_id, $client_id)
	{
	   $select  = $this->db->select()->from('acc_client_service');
	   $select->where('dpt_id = ?', (int)$dpt_id);
	   $select->where('client_id = ?', $client_id);
	   $select->order('link_id ASC')->limit(1);
	   
	   $rows = $this->db->fetchRow($select);
	   return $rows;
	}
	
	
  public function getClientServiceInfoByKey($client_id, $security_key)
  {
     $select  = $this->db->select()->from('acc_client_service');
     $select->where('client_id = ?', $client_id);
     $select->where('security_key = ?', $security_key);

     if (!empty($security_key)) $row = $this->db->fetchRow($select);
     else $row = false;
     //App_Log::query($this->db);

     return $row;
  
  }
  
  public function setSecurityKey($dpt_id, $dpt_account_id, $client_id, $url="")
  {
    $service_info = $this->getServiceInfo($dpt_id);
    //Zend_Debug::dump($service_info);
    $login_url = $service_info['remote_login'];
    if ($dpt_id == 2005 || $dpt_id == 2035)
    {
    	$login_area = $this->getLoginArea($dpt_id, $dpt_account_id, $client_id);
    	if ($login_area == 'admin')
    	{
    		$login_url = "http://admin.".$service_info['dpt_name']."/yesupCentralAccount_Clicksor.php";
    	}
    	if ($dpt_id==2005 and $login_area == 'advertiser')
    	{
    		$login_url = "http://admin.".$service_info['dpt_name']."/yesupCentralAccount_Clicksor.php";
    	}
    	
    }
    
    $skey = md5(time().rand());
    $rs = $this->updateKey($dpt_id, $dpt_account_id, $client_id, $skey);
    //Zend_Debug::dump($rs);exit;
    error_log("set skey ".$skey);
    
    $config = Zend_Registry::get('config');
    $return_url = $config->remote->callback->url;
    
     $return_url = ""; //&rurl=$return_url
     
    // for security, this url is hard code to every service backend login scripts
    if ($rs){
     	if (!empty($url)){
     		$url = urldecode($url);
      		$remote_cal_url = $login_url."?client_id=$client_id&skey=$skey&url=".urldecode($url);
     	}else
    		$remote_cal_url = $login_url."?client_id=$client_id&skey=$skey";
    }else $remote_cal_url = "";
  
    //echo $remote_cal_url;exit;
    return $remote_cal_url;
  }
	
	function updateKey($dpt_id, $dpt_account_id, $client_id, $skey = "")
	{
		$query = "update acc_client_service set security_key = '$skey' 
				  where dpt_id = '$dpt_id' and dpt_account_id = '$dpt_account_id' 
				  and client_id = '$client_id'  ";
		$updated = $this->db->query($query);
		//App_Log::query($this->db);

		if ($updated) return true;
		else return false;
	}	
	
	protected  function getServiceConn($dpt_id)
	{		
		$select  = $this->db->select()->from('acc_service_db');
		$select->where('status = ?', 'Active');
		$select->where('dpt_id = ?', (int)$dpt_id);
		error_log($select);
		$rows = $this->db->fetchAll($select);
		//Zend_Debug::dump($rows);
		if (!$rows) $rows = array();
		foreach ($rows as $row)
		{
			if (isset($row['dbhost_remote']) && !empty($row['dbhost_remote']) && APPLICATION_ENV == 'production') 
			{
				$dbInfo = array(
	          'host'     => $row['dbhost_remote'],
			  'port'	 => $row['dbhost_remote_port'],
	          'username' => $row['dbuser'],
	          'password' => $row['dbpwd'],
	          'dbname'   => $row['dbname']);
			}
			else 
			{
				$dbInfo = array(
	          'host'     => $row['dbhost'],
			  'port'	 => 3306,
	          'username' => $row['dbuser'],
	          'password' => $row['dbpwd'],
	          'dbname'   => $row['dbname']);
			}
			//Zend_Debug::dump($dbInfo);//exit;
			$db[$row['db_type']] = App_DBConn::getInstance($dbInfo);
		}
		//Zend_Debug::dump($db);exit;
		if (!$db) throw new Zend_Exception('No connection');
		
		//App_Log::query($this->db);

		return $db;
	}
	
	public function loginCheck($dpt_id, $dpt_account_id, $password)
	{	
		return ;
	}
	
	public function statusCheck($dpt_id, $dpt_account_login)
	{
		return;
	}
	
}